package com.jomoo.card.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.jomoo.card.service.CardService;
import com.jomoo.common.pojo.JoMooCard;
import com.jomoo.common.tools.ExcelTool;
import com.jomoo.common.utils.BaseAction;

/**
 * jomoo平台服务卡控制层
 * 
 * @author 郝思聪
 * @date 2016年12月21日
 */
@Scope("prototype")
@Component("CardAction")
public class CardAction extends BaseAction
{

	private static final long serialVersionUID = 765362796283103854L;

	Log log = LogFactory.getLog(CardAction.class);

	@Resource
	CardService cardService;
	
	/**
	 * 获取产品价格
	 * 
	 * @return String
	 */
	@Action(value = "getSpecSale")
	public void getSpecSale()
	{
		String strSpecId = request.getParameter("specid");
		Map<String, Float> map = new HashMap<String, Float>();
		Integer iSpecId = null;
		Float fSale = null;

		if (StringUtils.isNotBlank(strSpecId))
		{
			iSpecId = Integer.parseInt(strSpecId);
		}
		else
		{
			map.put("specsale", fSale);
			returnData(JSON.toJSONString(map));
		}

		try
		{
			// 从数据库中获取所有用户角色操作
			fSale = cardService.getSecondCity(iSpecId);

			if (null == fSale)
			{
				log.info("获取产品价格: 错误（表数据可能为空）！");
			}
			else
			{
				map.put("specsale", fSale);

				// 直接用fastjson解析为json字符串输出
				returnData(JSON.toJSONString(map));
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			log.error("查询产品价格异常：" + e.getMessage());
		}
	}

	/**
	 * 查询服务卡
	 */
	@Action(value = "getServiceCards")
	public void getServiceCards()
	{

		// 客户名
		String strPopName = request.getParameter("popName");

		// 客户电话
		String strPopPhone = request.getParameter("popPhone");

		// 厂商
		String strCustomerId = request.getParameter("customer");

		// 安装类型
		String strSetupType = request.getParameter("setuptype");

		// 安装状态
		String strSetupState = request.getParameter("setupstate");

		// 产品分类
		String strProductType = request.getParameter("classify");

		// 产品规格
		String strProductSpec = request.getParameter("spec");

		// 起始时间
		String strBeginTime = request.getParameter("begintime");

		// 终止时间
		String strEndTime = request.getParameter("endtime");

		Integer iCustomerId = null;
		Integer iSetupType = null;
		Integer iSetupState = null;
		Integer iProductType = null;
		Integer iProductSpec = null;

		if (StringUtils.isNotBlank(strCustomerId))
		{
			iCustomerId = Integer.parseInt(strCustomerId);
		}
		if (StringUtils.isNotBlank(strSetupType))
		{
			iSetupType = Integer.parseInt(strSetupType);
		}
		if (StringUtils.isNotBlank(strSetupState))
		{
			iSetupState = Integer.parseInt(strSetupState);
		}
		if (StringUtils.isNotBlank(strProductType))
		{
			iProductType = Integer.parseInt(strProductType);
		}
		if (StringUtils.isNotBlank(strProductSpec))
		{
			iProductSpec = Integer.parseInt(strProductSpec);
		}

		try
		{
			// 查询服务卡
			List<?> cardList = cardService.getServiceCards(strPopName, strPopPhone,
					iCustomerId, iSetupType, iSetupState, iProductType,
					iProductSpec, strBeginTime, strEndTime, null);

			// 直接用fastjson解析为json字符串输出
			returnData(JSON.toJSONString(cardList));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			log.error("查询服务卡异常：" + e.getMessage());
		}
	}
	
	/**
	 * 导出服务卡
	 */
	@Action(value = "getServiceCardExcle")
	public void getServiceCardExcle()
	{
		// 客户名
		String strPopName = request.getParameter("popName");

		// 客户电话
		String strPopPhone = request.getParameter("popPhone");

		// 厂商
		String strCustomerId = request.getParameter("customer");

		// 安装类型
		String strSetupType = request.getParameter("setuptype");

		// 安装状态
		String strSetupState = request.getParameter("setupstate");

		// 产品分类
		String strProductType = request.getParameter("classify");

		// 产品规格
		String strProductSpec = request.getParameter("spec");

		// 起始时间
		String strBeginTime = request.getParameter("begintime");

		// 终止时间
		String strEndTime = request.getParameter("endtime");

		Integer iCustomerId = null;
		Integer iSetupType = null;
		Integer iSetupState = null;
		Integer iProductType = null;
		Integer iProductSpec = null;


		if (StringUtils.isNotBlank(strCustomerId))
		{
			iCustomerId = Integer.parseInt(strCustomerId);
		}
		if (StringUtils.isNotBlank(strSetupType))
		{
			iSetupType = Integer.parseInt(strSetupType);
		}
		if (StringUtils.isNotBlank(strSetupState))
		{
			iSetupState = Integer.parseInt(strSetupState);
		}
		if (StringUtils.isNotBlank(strProductType))
		{
			iProductType = Integer.parseInt(strProductType);
		}
		if (StringUtils.isNotBlank(strProductSpec))
		{
			iProductSpec = Integer.parseInt(strProductSpec);
		}

		try
		{
			// 导出查询服务卡
			List<JoMooCard> cardList = cardService.getServiceCards(strPopName, strPopPhone,
					iCustomerId, iSetupType, iSetupState, iProductType,
					iProductSpec, strBeginTime, strEndTime, 1);
			
			ExcelTool.createExcel("平台卡单信息报表", "阶段内卡单信息汇总报表", cardList, log, "card");
		}
		catch (Exception e)
		{
			e.printStackTrace();
			log.error("服务卡导出异常：" + e.getMessage());
		}
	}

	/**
	 * 录入产品服务卡
	 * 
	 * @return String
	 */
	@Action(value = "addServiceCard")
	public void addServiceCard()
	{
		// 客户名
		String strPopName = request.getParameter("popname");

		// 客户电话
		String strPopPhone = request.getParameter("poPhone");

		// 客户产品
		String strProduct = request.getParameter("product");

		// 规格
		String strProSpec = request.getParameter("proSpec");

		// 一级城市
		String strFirstCity = request.getParameter("fcity");

		// 二级城市
		String strSecondCity = request.getParameter("scity");

		// 客户地址
		String strPopAddress = request.getParameter("popAddress");

		// 安装类型
		String strSetupType = request.getParameter("setuptype");

		// 安装时间
		String strSetupTime = request.getParameter("setuptime");

		// 产品价格
		String strMoney = request.getParameter("money");

		// 是否有单
		String strIsCard = request.getParameter("iscard");

		// 厂商
		String strCustomerId = request.getParameter("custid");
		
		String strCardFlag = request.getParameter("cardFlag");

		// 客户产品
		Integer iProduct = null;
		// 规格
		Integer iProSpec = null;
		// 一级城市
		Integer iFirstCity = null;
		// 安装类型
		Integer iSetupType = null;
		// 产品价格
		Float iMoney = null;
		// 是否有单
		Integer iIsCard = null;
		// 厂商
		Integer iCustomerId = null;
		
		Integer iCardFlag = null;

		if (StringUtils.isNotBlank(strCustomerId))
		{
			iCustomerId = Integer.parseInt(strCustomerId);
		}
		if (StringUtils.isNotBlank(strSetupType))
		{
			iSetupType = Integer.parseInt(strSetupType);
		}
		if (StringUtils.isNotBlank(strProduct))
		{
			iProduct = Integer.parseInt(strProduct);
		}
		if (StringUtils.isNotBlank(strProSpec))
		{
			iProSpec = Integer.parseInt(strProSpec);
		}
		if (StringUtils.isNotBlank(strFirstCity))
		{
			iFirstCity = Integer.parseInt(strFirstCity);
		}
		if (StringUtils.isNotBlank(strMoney))
		{
			iMoney = Float.parseFloat(strMoney);
		}
		if (StringUtils.isNotBlank(strCardFlag))
		{
			iCardFlag = Integer.parseInt(strCardFlag);
		}
		if(StringUtils.isNotBlank(strIsCard))
		{
			iIsCard = Integer.parseInt(strIsCard);
		}

		Map<String, Boolean> map = new HashMap<String, Boolean>();

		try
		{
			Integer iResult = cardService.addServiceCard(strPopName, strPopPhone,
					strPopAddress, strSetupTime, iProduct, iProSpec,
					iFirstCity, strSecondCity, iSetupType, iMoney, iIsCard,
					iCustomerId, iCardFlag);

			if (StringUtils.isNotEmpty(iResult.toString()))
			{
				map.put("result", true);
			}
			else
			{
				map.put("result", false);
			}

			// 直接用fastjson解析为json字符串输出
			returnData(JSON.toJSONString(map));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			log.error("录入服务卡异常：" + e.getMessage());
		}
	}

	/**
	 * 作废服务卡
	 */
	@Action(value = "delServiceCard")
	public void delServiceCard()
	{
		// 获取作废服务卡id
		String strCardId = request.getParameter("cardid");
		
		Integer iCardId = null;
		
		Map<String, Boolean> map = new HashMap<String, Boolean>();
		
		if (StringUtils.isNotBlank(strCardId))
		{
			iCardId = Integer.parseInt(strCardId);
		}

		try
		{
			// 执行作废服务卡操作
			Boolean bFlag = cardService.delServiceCard(iCardId);

			if (bFlag)
			{
				map.put("delflag", bFlag);

				// 直接用fastjson解析为json字符串输出
				returnData(JSON.toJSONString(map));
			}
			else
			{
				log.info("作废服务卡错误: 错误（表数据可能为空）！");
				
				map.put("delflag", bFlag);

				// 直接用fastjson解析为json字符串输出
				returnData(JSON.toJSONString(map));
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			log.error("作废服务卡异常：" + e.getMessage());
		}
	}
	
	/**
	 * 服务卡状态变更
	 */
	@Action(value = "updateServiceCardState")
	public void updateServiceCardState()
	{
		// 获取作废服务卡id
		String strCardId = request.getParameter("cardid");
		
		String strRemark = request.getParameter("remark");
		
		String strFlag = request.getParameter("flag");
		
		String strLazyTime = request.getParameter("lazytime");
		
		String strRevalue = request.getParameter("revalue");
		
		Integer iCardId = null;
		Integer iFlag = null;
		
		Map<String, Boolean> map = new HashMap<String, Boolean>();
		
		if (StringUtils.isNotBlank(strCardId))
		{
			iCardId = Integer.parseInt(strCardId);
		}
		
		if (StringUtils.isNotBlank(strFlag))
		{
			iFlag = Integer.parseInt(strFlag);
		}

		try
		{
			// 执行变更服务卡状态操作
			Integer iCount = cardService.updateCardState(iCardId, iFlag, strRemark, strLazyTime, strRevalue);

			if (iCount == 1)
			{
				map.put("delflag", true);

				// 直接用fastjson解析为json字符串输出
				returnData(JSON.toJSONString(map));
			}
			else
			{
				log.info("变更服务卡状态错误: 错误（表数据可能为空）！");
				
				map.put("delflag", false);

				// 直接用fastjson解析为json字符串输出
				returnData(JSON.toJSONString(map));
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			log.error("变更服务卡状态异常：" + e.getMessage());
		}
	}
	
	/**
	 * 查询服务卡详情
	 */
	@Action(value = "getServiceCardView")
	public void getServiceCardView()
	{
		// 获取服务卡id
		String strCardId = request.getParameter("cardid");
		
		Integer iCardId = null;
		
		if (StringUtils.isNotBlank(strCardId))
		{
			iCardId = Integer.parseInt(strCardId);
		}

		try
		{
			// 执行作废服务卡操作
			List<?> cardList = cardService.getCardViews(iCardId);

			if (cardList != null)
			{
				// 直接用fastjson解析为json字符串输出
				returnData(JSON.toJSONString(cardList));
			}
			else
			{
				log.info("服务卡详情错误: 错误（表数据可能为空）！");
				
				// 直接用fastjson解析为json字符串输出
				returnData(JSON.toJSONString(cardList));
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			log.error("服务卡详情异常：" + e.getMessage());
		}
	}
}
